import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  HttpStatus,
  Req,
  UploadedFile,
  Query,
  UseInterceptors,
} from '@nestjs/common';
import { UserService } from '../services/user.service';
import { CreateUserDto } from '../dtos/create-user.dto';
import { UpdateUserDto } from '../dtos/update-user.dto';
import { UploadDto } from '../dtos/upload.dto';
import { PaginationParamsDto } from '@/shared/dtos/pagination-params.dto';
import {
  ApiBasicAuth,
  ApiConsumes,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { FileInterceptor } from '@nestjs/platform-express';

@Controller('user')
@ApiTags('用户管理')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post()
  @ApiOperation({
    summary: '新增用户',
  })
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @Get()
  @ApiOperation({
    summary: '查找所有用户',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    type: CreateUserDto,
  })
  findAll(@Query() query: PaginationParamsDto) {
    // const { data, total } = this.userService.findAll(query);
    // return {
    //   data,
    //   meta: {
    //     total,
    //   },
    // };
  }

  @Get(':id')
  @ApiOperation({
    summary: '查找单个用户',
  })
  findOne(@Param('id') id: string) {
    return this.userService.findOne(+id);
  }

  @Patch(':id')
  @ApiOperation({
    summary: '更新单个用户',
  })
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto);
  }

  @Delete(':id')
  @ApiOperation({
    summary: '删除单个用户',
  })
  remove(@Param('id') id: string) {
    return this.userService.remove(+id);
  }
  @ApiConsumes('multipart/form-data')
  @UseInterceptors(FileInterceptor('file'))
  @Post('upload')
  @ApiOperation({
    summary: '上传头像',
  })
  async upload(
    @Req() req: any,
    @Body() uploadDto: UploadDto,
    @UploadedFile() file,
  ) {
    return this.userService.uploadAvatar(file);
  }
}
